package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.entity.MaterialSap;
import com.sunda.spmsorder.service.IMaterialSapService;
import com.sunda.spmsweb.util.BarCodeOverseaWhsInUtil;
import com.sunda.spmsweb.util.MatrixCodeBoxNoteUtil;
import com.sunda.spmsweb.util.MatrixCodeStorageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/barCode")
@Api(tags = "条形码接口", description = "条形码接口")
public class BarCodeController {

    @Autowired
    IMaterialSapService iMaterialSapService;

    @RequestMapping(value = "/createBarCodeBoxNote")
    @ApiOperation(value = "生成箱码条形码并返回信息", notes = "生成箱码条形码并返回\n" +
            "已存在条码图片会被覆盖。示例 \n" +
            "[{\n" +
            "\t\"boxNote\": \"ZX-10215800001-0404\",\n" +
            "\t\"relateDocNumber\": \"1180066382\"\n" +
            "}, {\n" +
            "\t\"boxNote\": \"ZX-10215800001-0403\",\n" +
            "\t\"relateDocNumber\": \"1180066382\"\n" +
            "}]", httpMethod = "POST")
    public ResponseResult createBarCodeBoxNote(@RequestBody JSONArray array) {
        if (array.size() == 0){
            return ResponseResult.error();
        }
        try {
            int result = 0;
            for (int i = 0; i < array.size(); i ++){
                String boxNote = array.getJSONObject(i).getString("boxNote");
                String relateDocNumber = array.getJSONObject(i).getString("relateDocNumber");
                relateDocNumber = StringUtils.isEmpty(relateDocNumber) ? "-" : relateDocNumber;
                if (StringUtils.isNotEmpty(boxNote)){
                    // 生成二维码不带图片
                    //BufferedImage image = MatrixCodeBoxNoteUtil.insertWords(MatrixCodeBoxNoteUtil.generateMatrixCode(boxNote), boxNote, relateDocNumber);
                    //BufferedImage image = MatrixCodeBoxNoteUtil.rotateImage(MatrixCodeBoxNoteUtil.insertImage(MatrixCodeBoxNoteUtil.insertWords(MatrixCodeBoxNoteUtil.generateMatrixCode(boxNote), boxNote, relateDocNumber), "BarCode/sunda.jpg", true), 90);
                    //ImageIO.write(image, "jpg", new File("BarCode/" + boxNote + ".jpg"));
                    MatrixCodeBoxNoteUtil.createMatrixWithoutPicture(boxNote, relateDocNumber);
                    result ++;
                }
            }
            return ResponseResult.success("请求成功，后台生成箱码条码数量为：" + result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
    }

    @RequestMapping(value = "/getBarCode", produces = MediaType.IMAGE_JPEG_VALUE)
    @ApiOperation(value = "获取条码图片", notes = "获取条码图片 \n" +
            "示例 箱码 = ZX-10215800001-0404\n" +
            "储位 = CN011061-A010101\n" +
            "海外收货 = BJ-11801149752-194200-2100013479 或 210004001", httpMethod = "GET")
    public byte[] getBarCodeImage(String docNumber) {
        try {
            File file = new File("BarCode/" + docNumber + ".jpg");
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
            return bytes;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value = "/createBarCodeStorageNo")
    @ApiOperation(value = "生成储位条形码并返回信息", notes = "生成箱码条形码并返回\n" +
            "已存在条码图片会被覆盖。示例 \n" +
            "[{\n" +
            "\t\"werks\": \"CN01\",\n" +
            "\t\"whsLocationCode\": \"1061\",\n" +
            "\t\"uuid\": \"b95946ac1dab42b79ade3438acb0b538\",\n" +
            "\t\"storageNo\": \"A010101\",\n" +
            "\t\"remarks\": \"A区01货架01层01格\"\n" +
            "}, {\n" +
            "\t\"werks\": \"CN01\",\n" +
            "\t\"whsLocationCode\": \"1061\",\n" +
            "\t\"uuid\": \"cd8a479246954765bc927f992f368987\",\n" +
            "\t\"storageNo\": \"A020101\",\n" +
            "\t\"remarks\": \"A区02货架01层01格\"\n" +
            "}]", httpMethod = "POST")
    public ResponseResult createBarCodeStorageNo(@RequestBody JSONArray array) {
        if (array.size() == 0){
            return ResponseResult.error();
        }
        try {
            int result = 0;
            for (int i = 0; i < array.size(); i ++){
                String werks = array.getJSONObject(i).getString("werks");
                String whsLocationCode = array.getJSONObject(i).getString("whsLocationCode");
                String uuid = array.getJSONObject(i).getString("uuid");
                String storageNo = array.getJSONObject(i).getString("storageNo");
                String remarks = array.getJSONObject(i).getString("remarks");
                remarks = StringUtils.isEmpty(remarks) ? "" : remarks;
                if (StringUtils.isNotEmpty(uuid)){
                    String fileName = werks + whsLocationCode + "-" + storageNo;
                    /***
                     * 生成条形码
                     * BufferedImage image = BarCodeStorageUtil.insertWords(BarCodeStorageUtil.getBarCode(uuid), werks, whsLocationCode, uuid, storageNo, remarks);
                     */
                    // 生成二维码不带图片
                    //BufferedImage image = MatrixCodeStorageUtil.insertWords(MatrixCodeStorageUtil.generateMatrixCode(uuid, storageNo), werks, whsLocationCode, uuid, storageNo, remarks);
                    //BufferedImage image = MatrixCodeStorageUtil.rotateImage(MatrixCodeStorageUtil.insertImage(MatrixCodeStorageUtil.insertWords(MatrixCodeStorageUtil.generateMatrixCode(uuid, storageNo), werks, whsLocationCode, uuid, storageNo, remarks), "BarCode/sunda.jpg", true), 90);
                    //ImageIO.write(image, "jpg", new File("BarCode/" + fileName + ".jpg"));
                    MatrixCodeStorageUtil.createMatrixWithoutPicture(werks, whsLocationCode, uuid, storageNo, remarks);
                    result ++;
                }
            }
            return ResponseResult.success("请求成功，后台生成储位条码数量为：" + result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
    }

    @RequestMapping(value = "/generateBarCodeWhsIn")
    @ApiOperation(value = "生成海外入库物料条形码", notes = "生成海外入库物料条形码\n" +
            "已存在条码图片会被覆盖。示例 \n" +
            "{\n" +
            "\t\"createDate\": \"2022-01-17\",\n" +
            "\t\"materialNoList\": [{\n" +
            "\t\t\"materialNo\": \"210001347\",\n" +
            "\t\t\"boxNote\": \"BJ-11801149752-194200\"\n" +
            "\t}, {\n" +
            "\t\t\"materialNo\": \"210004001\",\n" +
            "\t\t\"boxNote\": \"BJ-11801149752-194200\"\n" +
            "\t}, {\n" +
            "\t\t\"materialNo\": \"210004929\",\n" +
            "\t\t\"boxNote\": \"\"\n" +
            "\t}]\n" +
            "}\n" +
            "箱码不为空图片名称为：boxNote + - + materialNo + .jpg；如：BJ-11801149752-194200-210001347.jpg；\n" +
            "箱码为空图片名称为：materialNo + .jpg；如：210004929.jpg；\n" +
            "", httpMethod = "POST")
    public ResponseResult generateBarCodeWhsIn(@RequestBody JSONObject doc) {
        if (!doc.containsKey("createDate") || StringUtils.isEmpty(doc.getString("createDate"))){
            return ResponseResult.error("创建日期不允许为空");
        }
        JSONArray materialNoList = doc.getJSONArray("materialNoList");
        if (materialNoList.isEmpty()){
            return ResponseResult.error("打印明细信息不允许为空");
        }
        try {
            int result = 0;
            String createDate = doc.getString("createDate");
            for (int i = 0; i < materialNoList.size(); i ++){
                String materialNo = materialNoList.getJSONObject(i).getString("materialNo");
                if (StringUtils.isNotEmpty(materialNo)){
                    MaterialSap materialSap = iMaterialSapService.getMaterialInfo(materialNo);
                    if (materialSap != null){
                        String materialZhDesc = materialSap.getMaterialZhDesc();
                        String materialEnDesc = materialSap.getMaterialEnDesc();
                        String boxNote = materialNoList.getJSONObject(i).getString("boxNote");
                        BarCodeOverseaWhsInUtil.generateBarCodeWhsIn(materialNo, materialZhDesc, materialEnDesc,boxNote, createDate);
                        result ++;
                    }
                }
            }
            return ResponseResult.success("请求成功，后台生成物料条码数量为：" + result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error();
        }
    }


}
